package com.hxkj.service.article.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ColumnServiceClient;
import com.hxkj.client.service.TagsClassifyServiceClient;
import com.hxkj.domain.enums.ColumnExceptionEnum;
import com.hxkj.domain.enums.TagsClassifyExceptionEnum;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Channel;
import com.hxkj.domain.po.TagsClassify;
import com.hxkj.service.article.TagsClassifyService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagsClassifyServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 12:01
 * @Version 1.0
 */

@Service
@Slf4j
public class TagsClassifyServiceImpl implements TagsClassifyService {

    @Autowired
    private TagsClassifyServiceClient tagsClassifyServiceClient;

    @Override
    public void saveTagsClassify(TagsClassify tagsClassify) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tagsClassify == null) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_VOID);
            }
            if (StringUtils.isEmpty(tagsClassify.getClassifyName())) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_NAME);
            }
            if (tagsClassify.getSiteId() == null) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_SITEID);
            }
            resultMessageStruct = tagsClassifyServiceClient.saveTagsClassify(tagsClassify);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<TagsClassify> getTagsClassifyList(TagsClassify tagsClassify, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<TagsClassify> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(tagsClassify);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<TagsClassify> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = tagsClassifyServiceClient.getTagsClassifyList(parameterDataStruct);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签分类列表失败");
        }
        return resultPageStruct;
    }

    @Override
    public void removeTagsClassify(TagsClassify tagsClassify) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tagsClassify == null) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_VOID);
            }
            if (null == tagsClassify.getTagsClassifyIds() || StringUtils.isEmpty(tagsClassify.getTagsClassifyIds())) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_VOID);
            }
            resultMessageStruct = tagsClassifyServiceClient.removeTagsClassify(tagsClassify);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifyTagsClassify(TagsClassify tagsClassify) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (tagsClassify == null) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_VOID);
            }
            if (tagsClassify.getSiteId() == null) {
                throw new CustomException(TagsClassifyExceptionEnum.TAGS_CLASSIFY_NOT_SITEID);
            }
            resultMessageStruct = tagsClassifyServiceClient.modifyTagsClassify(tagsClassify);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(TagsClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<TagsClassify> getTagsClassifyDownList(TagsClassify tagsClassify) {
        List<TagsClassify> tagsClassifyList = new ArrayList<>();
        try {
            tagsClassifyList = tagsClassifyServiceClient.getTagsClassifyDownList(tagsClassify);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签分类列表失败");
        }
        return tagsClassifyList;
    }
}
